<?php
namespace Api\Controller;
class MessageController extends ApiController{
    /**
     * 消息列表
     * @param int page
     * @param int pagesize
     * @param int type
     */
    public function messageList(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
        $type = I('post.type');
        $page = I('post.page', 1);
        $page <= 0 && $page = 1;
        $pagesize = I('post.pagesize', 10);
        $pagesize <= 0 && $pagesize = 10;
        $offset = ($page - 1) * $pagesize;
        $M_message = M('message');
        $map = array();
        $map['uid'] = API_UID;
        $total = $M_message->where($map)->count();
        $list = $M_message->where($map)->limit($offset, $pagesize)->field('id,title,message,create_time time,is_read,type,cover_id image_path')->order('id DESC')->select();
        foreach ($list as &$item) {
            $item['image_path'] = get_url($item['image_path']);
        }
        $result = array();
        $result['total'] = $total;
        $result['result'] = $list;
        response(1, $result);
    }
    
    /**
     * 消息列表
     * @param int id
     */
    public function messageDetail(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
        params_exist(array('id'));
        $id = I('post.id');
        $M_message = M('message');
        $map = array();
        $map['id'] = $id;
        $map['uid'] = API_UID;
        $info = $M_message->where($map)->field('title,message,create_time time,is_read')->find();
        $info || response(-1, null, '消息不存在');
        $M_message->where($map)->save(array('read_time'=>NOW_TIME,'is_read'=>1));
        $info ? response(1, $info) : response(- 2);
    }
}
?>